<?php

namespace App\Http\Controllers;

use App\Http\Resources\TopicResource;
use App\Models\Topic;
use App\Http\Requests\StoreTopicRequest;
use App\Http\Requests\UpdateTopicRequest;
use Illuminate\Support\Facades\Auth;

class TopicController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth:sanctum')->except(['index', 'show']);
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
//        $collection = System::orderBy('order','asc')->get();
//        return SystemResource::collection($collection);
        $collection = Topic::orderBy('created_at', 'desc')->with('user')->when(request('uid'), function ($query) {
            return $query->where('user_id', request('uid'));
        })->paginate(10);
        return TopicResource::collection($collection);
        //
    }


    /**
     * Store a newly created resource in storage.
     *
     * @param \App\Http\Requests\StoreTopicRequest $request
     * @return \Illuminate\Http\Response
     */
    public function store(StoreTopicRequest $request, Topic $topic)
    {
        $topic->fill($request->input());
        $topic->user_id = Auth::id();
        $topic->save();
        return $this->success('帖子发表成功', $topic->load('user'));
    }

    public function show(Topic $topic)
    {


        $topic->html = $topic->html;
        return new TopicResource($topic->load('user'));
        //
    }


    public function update(UpdateTopicRequest $request, Topic $topic)
    {
        $this->authorize('update', $topic);
        $topic->fill($request->input())->save();
        return $this->success('文章更新成功', $topic);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param \App\Models\Topic $topic
     * @return \Illuminate\Http\Response
     */
    public function destroy(Topic $topic)
    {
        $this->authorize('delete', $topic);
        $topic->delete();
        return $this->success('删除成功');
        //
    }
}
